package com.medical.sys.web.controller;

import com.medical.common.JsonResult;
import com.medical.common.TreeNode;
import com.medical.sys.model.Organization;
import com.medical.sys.model.User;
import com.medical.sys.service.OrganizationService;
import com.medical.sys.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2016/10/16.
 */
@Controller
@RequestMapping("/organization")
//门店管理
public class OrganizationContorller {

    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private UserService userService;

    @ApiOperation(value="返回TreeNode<oraganization>集合", notes="把查询到的oraganization表中总数据的id，name，peopleId加到TreeNode中，返回TreeNode集合")
    @RequestMapping(value = "/getlist", method = RequestMethod.GET)
    @ResponseBody
    //把查询到的oraganization表中总数据的id，name，peopleId加到TreeNode中，返回TreeNode集合
    public List<TreeNode> list() {
        try {
            return organizationService.getList();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    //根据organizationId返回相关的user对象集合
    @ApiOperation(value="返回user集合", notes="根据organizationId返回相关的user对象集合")
    @RequestMapping(value = "/getuser/{orgid}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getusers(@PathVariable String orgid){
        try{
            List<User> users=userService.selectByOrgId(orgid);
            return new JsonResult("200","",users);
        }catch (Exception e){
            return new JsonResult("500",e.getMessage());
        }
    }

    //返回的是携带这个Organization的UserId集合的Organization对象
    @ApiOperation(value="返回Organization对象", notes="返回的是携带这个Organization的UserId集合的Organization对象")
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult get(@PathVariable String id) {
        try {
            return new JsonResult("200", "", organizationService.getOrganization(id));
        } catch (Exception e) {
            return new JsonResult("500", e.getMessage());
        }
    }

    /**
     * @param name     String
     * @param parentid String
     * @return
     */

    //新增Organization表的数据
    @ApiOperation(value="新增Organization表的数据", notes="新增Organization表的数据")
    @RequestMapping(value = "/post", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult postOrganization(@RequestParam(value = "name", required = false) String name,
                                       @RequestParam(value = "parentid", required = false) String parentid
                                    ) {

        Organization organization = new Organization();
        organization.setName(name);
        organization.setParentId(parentid);
        try {
            organizationService.saveOrganization(organization);
            return new JsonResult("200", "", organization);
        } catch (Exception e) {
            return new JsonResult("500", e.getMessage());
        }
    }

    /**
     * @param id       String
     * @param name     String
     * @param parentid String
     * @return
     */
    //根据id和parentid更新Organization的数据
    @ApiOperation(value="更新Organization的数据", notes="根据id和parentid更新Organization的数据")
    @RequestMapping(value = "/patch/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult edit(@PathVariable String id, @RequestParam(value = "name", required = false) String name,
                           @RequestParam(value = "parentid", required = false) String parentid) {
        Organization organization = organizationService.getOrganization(id);
        organization.setName(name);
        organization.setParentId(parentid);
        try {
            organizationService.updateOrganization(organization);
            return new JsonResult("200", "", organization);
        } catch (Exception e) {
            return new JsonResult("500", e.getMessage());
        }
    }
    //根据id和name更新Organization的数据
    @ApiOperation(value="更新Organization的数据", notes="根据id和name更新Organization的数据")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult updateName(@PathVariable String id,
                                 @RequestParam(value = "name", required = false) String name) {
        Organization organization = organizationService.getOrganization(id);
        organization.setName(name);
        try {
            organizationService.updateOrganization(organization);
            return new JsonResult("200", "", organization);
        } catch (Exception e) {
            return new JsonResult("500", e.getMessage());
        }
    }
    //删除
    @ApiOperation(value="删除", notes="根据id删除organization对象")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult deleteOrganization(@PathVariable String id) {
        try {
            organizationService.deleteOrganization(id);
            return new JsonResult("200", "success");
        } catch (Exception e) {
            return new JsonResult("500", e.getMessage());
        }
    }
    //暂时保留，未用到此类
    @RequestMapping(value="/drop",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult drop(@ModelAttribute("treeNode") TreeNode treeNode,HttpServletRequest req){
        organizationService.moveOrganization(treeNode.getId(),treeNode.getTargetId(),treeNode.getMoveType());
        return new JsonResult();
    }

}
